package com.pn.service.impl;

import com.pn.entity.Product;
import com.pn.entity.ProductType;
import com.pn.entity.Result;
import com.pn.mapper.ProductTypeMapper;
import com.pn.page.Page;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.pn.mapper.ProductMapper;
import com.pn.service.ProductService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService{

    @Resource
    private ProductMapper productMapper;
    @Value("${file.access-path}")
    private String filePath;
    @Override
    public Page findAllProductByPage(Product product, Page page) {
        int countProductNum = productMapper.countProductNum(product);
        List<Product> productList = productMapper.selectAllProductByPage(product, page);
        page.setTotalNum(countProductNum);
        page.setResultList(productList);
        return page;
    }

    @Override
    public Result addProduct(Product product) {
        //根据产品型号查询是否重复
        Product productByNum = productMapper.selectProductByNum(product.getProductNum());
        product.setImgs(filePath + product.getImgs());
        if(productByNum == null){
            int result = productMapper.insertProduct(product);
            if (result > 0){
                return Result.ok("产品插入成功！");
            }else {
                return Result.err(Result.CODE_ERR_BUSINESS,"产品插入失败！");
            }
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"产品已存在");
        }
    }

    @Override
    public int modifyStateByPid(Product product) {
        int i = productMapper.updateStateByPid(product);
        return i;
    }

    @Override
    public int removeProduct(Integer[] pid) {
        return productMapper.deleteProduct(pid);
    }

    @Override
    public Result modifyProduct(Product product) {
        Product productByNum = productMapper.selectProductByNum(product.getProductNum());
        if (productByNum != null){
            return Result.err(Result.CODE_ERR_BUSINESS,"产品型号已存在！");
        }
        if(!product.getImgs().contains(filePath)){
            product.setImgs(filePath + product.getImgs());
        }
        int result = productMapper.updateProduct(product);
        if (result > 0){
            return Result.ok("产品信息修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"产品信息修改失败！");
    }













}
